// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import Tool from "../../framework/Tool";
const {ccclass, property} = cc._decorator;

@ccclass
export default class handSwitch extends cc.Component {

    @property(cc.SpriteFrame)
    skinList : cc.SpriteFrame[] = [];

    @property(cc.Sprite)
    picNode : cc.Sprite = null;

    @property(cc.Node)
    pieceNode : cc.Node = null;

    clickTimes = 0;

    onLoad () {
        if(gameData.colorfulPiece[0][0] == 0){
            this.node.on("touchstart",this.clickEvent,this);
        }
    }

    clickEvent(event){
        if(this.clickTimes > 5){
            this.pieceNode.active = true;
            gameData.colorfulPiece[0][0] = 1;
            gameData.saveColorfulPiece();
            cc.tween(this.pieceNode).by(0.5,{
                position : cc.v2(0,-cc.winSize.height / 2)
            }).call(()=>{
                cc.tween(this.pieceNode).to(0.5,{
                    position : cc.v2(0,0)
                }).call(()=>{
                    this.scheduleOnce(()=>{
                        this.pieceNode.active = false;
                    },1);
                    Tool.loadPrefab("prefabs/getSkin",res=>{
                        let view = cc.instantiate(res);
                        if(gameData.getColorfulPieceNumber(gameData.colorfulPiece[0]) == 7){
                            view.getComponent("getSkinMediator").setCallBack(true,1);
                        }else{
                            view.getComponent("getSkinMediator").setCallBack(false,1);
                        }
                        view.parent = cc.find("Canvas");
                    });
                }).start();
            }).start();
            this.node.off("touchstart",this.clickEvent,this);
        }
        if(this.picNode.node.angle == 0){
            this.picNode.node.angle = 30;
        }else {
            this.picNode.node.angle = 0;
        }
        this.clickTimes += 1;
        this.picNode.spriteFrame =  this.skinList[this.clickTimes];
    }
}
